package Library;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		MyLibraryDao library = new MyLibraryDaoImpl();// 根据需求选择
		ShelfDao shelf = new ShelfDaoImpl();

		String str = "";
		try {
			Scanner sc1 = new Scanner(new File("book.txt"));
			String choose;
			while (sc1.hasNextLine()) {
				str = sc1.nextLine();
				String[] temp = str.split("#");
				Book x1 = new Book(Integer.parseInt(temp[0]), temp[1], temp[2], temp[3], temp[4]);
				shelf.addBook(x1);

			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}

		// Book x1 = new Book(1, "解忧杂货店", "东野圭吾", "咚咚出版社", "A");调试数据
		// Book x2 = new Book(4, "解忧杂货店", "东野圭吾", "咚咚出版社", "A");
		// Book x3 = new Book(3, "解忧杂货店", "东野圭吾", "咚咚出版社", "A");
		// shelf.addBook(x1);
		// shelf.addBook(x2);
		// shelf.addBook(x3);
		int choice;
		Scanner sc = new Scanner(System.in);

		while (true) {
			Date date = new Date();// 取当前时间
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 转换时间的格式
			System.out.println(formatter.format(date));
			shelf.showBook();// 展示书架上的书籍

			System.out.println("请选择：");
			System.out.println(
					"1.借书        /               2.查找书籍              /               3.进入自己的图书馆                /            其他按键：退出");
			choice = sc.nextInt();
			if (choice == 1) {// 添加书籍到自己的图书馆
				System.out.println("请选择你要借阅的图书id:");
				int input = sc.nextInt();
				if (shelf.getItemById(input) == null) {
					System.out.println("书籍不存在！");
					continue;
				} else if (!shelf.getItemById(input).getState().equals("可借阅")) {
					System.out.println("该书籍已被借阅，请等待其他借阅者归还后再借阅");

				} else {
					if (library.borrowBook(shelf.getItemById(input))) {
						System.out.println("图书借阅成功！快去康康叭！");
					}
				}

			} else if (choice == 2) {
				
				int flag=0;
				while(true) {
					System.out.println("请选择：");
					System.out.println("1.根据id找书       /            2.根据书名找书       /           3.根据作者找书/           其他按键：退出查找");
					int input = sc.nextInt();
					switch (input) {
					case 1:
						System.out.println("请输入id：");
						int inputId=sc.nextInt();
						shelf.getItemById(inputId).toString();

						break;
					case 2:
						System.out.println("请输入书名：");
						String inputName=sc.next();
						shelf.findBook(inputName,1);

						break;
					case 3:
						System.out.println("请输入作者名字：");
						String inputAuthor=sc.next();
						shelf.findBook(inputAuthor,2);

						break;

					default:
						flag=1;
						break;
					}
					if(flag==1) {
						break;
					}
				}
				

			} else if (choice == 3) {// 进入自己的图书馆
				while (true) {
					library.showAll();// 展示自己的图书馆
					System.out.println("请选择：");
					System.out.println("1.还书       /           其他按键：退出自己的图书馆");
					int in = sc.nextInt();
					if (in == 1) {
						System.out.println("请选择你要归还的图书id:");
						int choose1 = sc.nextInt();
						if (library.returnBook(shelf.getItemById(choose1))) {
							System.out.println("图书归还成功！");
						} else {
							System.out.println("图书归还失败，请检查id是否正确~");
						}
					} else {
						break;
					}

				}

			} else {
				break;
			}

		}

	}

}
